﻿using System;
using System.Collections.Generic;
using System.Xml.XPath;

namespace Quail
{
    /// <summary>
    /// Delegate signature for the clear cache event
    /// </summary>
    public delegate void OnClearCacheEvent();

    /// <summary>
    /// Interface for all Quail controls
    /// </summary>
    public interface IXmlNode
    {
        /// <summary>
        /// Gets a value indicating whether this <see cref="IXmlNode"/> is exists.
        /// </summary>
        /// <value><c>true</c> if exists; otherwise, <c>false</c>.</value>
        bool Exists { get; }

        /// <summary>
        /// Gets or sets the selector.
        /// </summary>
        /// <value>The selector.</value>
        string Selector { get; set; }

        /// <summary>
        /// Gets the page.
        /// </summary>
        /// <value>The page.</value>
        BasicWebPage Page { get; }

        /// <summary>
        /// Gets the browser.
        /// </summary>
        /// <value>The browser.</value>
        BasicWebBrowser Browser { get; }

        /// <summary>
        /// Gets the active task.
        /// </summary>
        /// <value>The active task.</value>
        Task ActiveTask { get; }

        /// <summary>
        /// Gets the parent node.
        /// </summary>
        /// <value>The parent node.</value>
        IXmlNode ParentNode { get; }

        /// <summary>
        /// This event is fired when the control should release any cached objects
        /// </summary>
        event OnClearCacheEvent OnClearCache;

        /// <summary>
        /// Get a node from the XML based on the given selector
        /// </summary>
        /// <param name="selector">The XML selector</param>
        /// <returns>The node matching the given selector in the XML</returns>
        IXmlNode GetNode(string selector);

        /// <summary>
        /// Get a node from the XML based on the given selector
        /// </summary>
        /// <param name="selector">The XML selector</param>
        /// <returns>The node matching the given selector in the XML</returns>
        IEnumerable<IXmlNode> GetNodes(string selector);

        /// <summary>
        /// Get a list of items from the XML based on the given selector
        /// </summary>
        /// <param name="selector">The XML selector</param>
        /// <param name="creator">Creator method</param>
        /// <returns>The node matching the given selector in the XML</returns>
        List<T> GetList<T>(string selector, Func<XPathNavigator, string, T> creator);

        /// <summary>
        /// Get a value from the XML based on the given selector
        /// </summary>
        /// <typeparam name="T">The type of the value to get</typeparam>
        /// <param name="selector">The XML selector</param>
        /// <returns>The value of the given selector in the XML</returns>
        T GetValue<T>(string selector);

        /// <summary>
        /// Get a value from the XML of the given node
        /// </summary>
        /// <typeparam name="T">The type of the value to get - string, int, decimal or bool</typeparam>
        /// <returns>The value of the given selector in the XML</returns>
        T GetValue<T>();

        /// <summary>
        /// Get a list of string values found with the given xml element name
        /// </summary>
        /// <param name="selector">The XPath selector</param>
        /// <returns>The list of strings</returns>
        List<T> GetValues<T>(string selector);
    }
}
